package com.common.widget;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.utils.StarAppUtils;
import com.starappallcommon.R;


/**
 * 时间选择控件
 * 
 * <br>Created 2014-6-6 下午5:24:55
 * @version  
 * @author   linsj		
 *
 * @see
 */
public class WheelTimePicker extends LinearLayout {
	/**wheelview的tag，表示滑动的是小时滚轮*/
	private final int WHEEL_VIEW_TYPE_HOUR = 1;
	/**wheelview的tag，表示滑动的是分钟滚轮*/
	private final int WHEEL_VIEW_TYPE_MINUTE = 2;
	/**wheelview的tag，表示滑动的是上下午的滚轮*/
	private final int WHEEL_VIEW_TYPE_MERIDIAN= 3;
	
	/**上下午各12个小时*/
	private final int HOUR_COUNT = 12;
	/**60分钟*/
	private final int MINUTE_COUNT = 60;
	
	/** 上下文 */
	private Context mContext;

	/** 时间 */
	private Calendar mCalendar;

	/** 小时滚轮 */
	private WheelView mWvHour;
	/** 分钟滚轮 */
	private WheelView mWvMinute;
	/** 上下午滚轮 */
	private WheelView mWvMeridian;

	/** 文字大小 */
	private final int TEXT_SIZE = 18;
	private final int HOURS = 12;
	/** 当前选中Item的字体颜色 */
	private static final int CUR_COLOR = 0xFF000000;

	/**
	 * 初始化日期滚轮控件
	 * 
	 * Creates a new instance of WheelTimePicker.
	 * <br>Created 2014-6-6 下午5:27:50
	 * @param context 上下文
	 */
	public WheelTimePicker(Context context) {
		this(context, null);
	}

	/**
	 * 初始化日期滚轮控件
	 * 
	 * Creates a new instance of WheelTimePicker.
	 * <br>Created 2014-6-6 下午5:28:10
	 * @param context  上下文
	 * @param attrs 属性集
	 */
	public WheelTimePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.wheel_time_picker, this, true);

		mWvHour = (WheelView) findViewById(R.id.wheel_hour);
		mWvHour.setTag(WHEEL_VIEW_TYPE_HOUR);
		
		mWvMinute = (WheelView) findViewById(R.id.wheel_minute);
		mWvMinute.setTag(WHEEL_VIEW_TYPE_MINUTE);
		
		mWvMeridian = (WheelView) findViewById(R.id.wheel_which_meridian);
		mWvMeridian.setTag(WHEEL_VIEW_TYPE_MERIDIAN);

		// hour
		mWvHour.setCyclic(true);
		mWvHour.addScrollingListener(scrollListener);

		// minute
		mWvMinute.setCyclic(true);
		mWvMinute.addScrollingListener(scrollListener);
		
		mWvMeridian.setCyclic(false);
		mWvMeridian.addScrollingListener(scrollListener);
		
		initData();
	}
	
	/**
	 * 滚轮上下滑动监听
	*/
	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {

		@Override
		public void onScrollingStarted(WheelView wheel) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			//滚轮滑动结束
			if(WHEEL_VIEW_TYPE_HOUR == (Integer)wheel.getTag()){
				mCalendar.set(Calendar.HOUR, wheel.getCurrentItem());
			}else if(WHEEL_VIEW_TYPE_MINUTE == (Integer)wheel.getTag()){
				mCalendar.set(Calendar.MINUTE, wheel.getCurrentItem());
			}else if(WHEEL_VIEW_TYPE_MERIDIAN == (Integer)wheel.getTag()){
				if(0 == wheel.getCurrentItem()){
					mCalendar.set(Calendar.AM_PM, Calendar.AM);
				}else{
					mCalendar.set(Calendar.AM_PM, Calendar.PM);
				}
			}
			initData();
		}
	};

	/**
	 * 设置时间
	 * 
	 * <br>Created 2014-6-6 下午5:28:33
	 * @param calendar 日期
	 * @author        :  linsj
	 */
	public void setTime(Calendar calendar) {
		mCalendar = calendar;
		initData();
	}

	/**
	 * @n<b>函数名称</b>     :initData
	 * @brief　　初始化滚轮数据
	 * @see
	 * @since    Ver 1.1
	 * @<b>作者</b>          :  linsj
	 * @<b>创建时间</b>      :  2014-4-24下午7:04:51      
	*/
	private void initData() {
		if(null == mCalendar){
			return;
		}
		
		// 显示的item数量
		final int visibleItemCount = 3;
		
		// 初始化小时滚轮
		String[] listHours = new String[HOUR_COUNT];

		// 先加入12点
		listHours[0] = Integer.toString(HOURS);
		// 再加入1　-　11点
		for (int index = 1; index < HOUR_COUNT; index++) {
			listHours[index] = Integer.toString(index);
		}
		
		mWvHour.setViewAdapter(new TimeAdapter(mContext,
				mCalendar.get(Calendar.HOUR), listHours, mCalendar));
		mWvHour.setCurrentItem(mCalendar.get(Calendar.HOUR));
		mWvHour.setVisibleItems(visibleItemCount);
		
		// 初始化分钟滚轮
		String[] listMinutes = new String[MINUTE_COUNT];
		for (int index = 0; index < MINUTE_COUNT; index++) {
			listMinutes[index] = Integer.toString(index);
		}
		mWvMinute.setViewAdapter(new TimeAdapter(mContext, mCalendar
				.get(Calendar.MINUTE), listMinutes, mCalendar));
		mWvMinute.setCurrentItem(mCalendar.get(Calendar.MINUTE));
		mWvMinute.setVisibleItems(visibleItemCount);
		
		// 初始化上下午滚轮
		String[] listMeridian = new String[2];
		listMeridian[0] = mContext.getResources().getString(R.string.calendar_am);
		listMeridian[1] = mContext.getResources().getString(R.string.calendar_pm);
		int iCurrentItem;
		if (Calendar.AM == mCalendar.get(Calendar.AM_PM)) {
			iCurrentItem = 0;
		} else {
			iCurrentItem = 1;
		}
		mWvMeridian.setViewAdapter(new TimeAdapter(mContext, iCurrentItem, listMeridian,
				mCalendar));
		mWvMeridian.setCurrentItem(iCurrentItem);
		mWvMeridian.setVisibleItems(visibleItemCount);
	}

	/**
	 * ClassName:TimeAdapter
	 * Function: 时间适配器
	 * Reason:	 TODO ADD REASON
	 * 
	 * @author   Administrator		
	 * @version  WheelTimePicker
	 * @since    Ver 1.1
	 * @Date	 2014	2014-4-24		下午7:05:19
	 *
	 * @see 	 
	 */
	private class TimeAdapter extends ArrayWheelAdapter<String> {
		// Index of current item
		int currentItem;
		// value of current item
		int miCurrentValue;

		/**
		 * 
		 * Creates a new instance of TimeAdapter.
		 * <br>Created 2014-6-6 下午5:26:54
		 * @param context 上下文
		 * @param iCurrentIndex 当前索引
		 * @param items   列表
		 * @param calendar 日历
		 */
		public TimeAdapter(Context context, int iCurrentIndex, String[] items, Calendar calendar) {
			super(context, items, calendar);
			setTextSize(TEXT_SIZE);
			miCurrentValue = iCurrentIndex;
		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			//当前值与初始化时传入的值一样时，设为黑体
			if (currentItem == miCurrentValue) {
				view.setTextColor(CUR_COLOR);
			}
			StarAppUtils.setViewTypeFace(view, false);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			currentItem = index;
			return super.getItem(index, cachedView, parent);
		}
	}
}
